// pages/addGood/addGood.js
import {
  addMerchant,
  // systematicsList,
  // addEdit,
  getToken,
  productDetail,
  infoGoodsDetail,
  saveEdit,
  delGoods,
  addEditProduct
} from '../../../api.js'
let app = getApp();
Page({
  data: {
    disabled: false, //查看商品详情
    isEdit: false, //编辑商品
    type: 0,
    isChecked: 0, //商品类型选择
    isChecked2: 2, //商品保养选择
    isChecked3: 2, //上下架选择
    isChecked4: 2, //入库同步选择
    showCommQuality: false,
    showLibraryAge: false,
    showTradePrice: false,
    liemu1: false,
    show1: false,
    show2: false,
    show3: false,
    show4: false,
    liemu2: false,
    liemu3: false,
    liemu4: false,

    showImages: [], //显示图片
    productFineness: [], //商品成色说明
    perchaseSource: [], //商品采购类别
    warehouseCondition: [], //商品库存状态
    pro_index: -1,
    war_index: -1,
    per_index: -1,
    sourceFocus: false, //商品来源自动聚焦
    addEdit: {
      image: [],
      product_cate: 0, //商品类型 0 二手 1 全新
      finess_value: '', //商品成色  
      product_condition: '', //商品成色
      is_shelf: '', //库存状态
      wc_value: '', //库存状态（文字）
      procurement_value: '', //采购类别
      procurement_status: '', //采购类别
      goodsLeibie: '', //商品类别
      cag_title: '',
      type_title: '',
      brand_title: '',
      systematics_title: '',
      category_id: '', //大类目
      type_id: '', //小类目
      brand_id: '', //品牌
      systematics_id: '', //系列
      product_sn: '', //商品货号
      product_name: '', //商品名称
      product_code: '', //商品编码
      maintenance: 2, //商品保养情况
      store_name: '', //所在门店
      store_id: '', //所在门店 
      coordinate: '', //商品坐标
      library_age: '', //预警库龄
      remark: '', //备注
      cost_price: '', //采购价
      additional: '', //额外费用
      product_source: '', //商品来源
      staff_name: '', //采购员
      staff_id: '', //采购员    
      price: '', //销售价
      peer_price: '', //同行价
      is_onsale: 2, //是否上架 2 下 1 上
      is_network: 2, //是否入全网 2否 1 是
      stock: 1, //库存数量*（定值）
      activity_price: '', //活动价
      market_price: '', //市场价
      product_service: ['专业鉴定', "非质量问题不退换", "会员免邮"], //商品服务
      group_people: '男女通用', //适用人群
      color: '', //颜色
      material: '', //材质
      size: '', //配饰尺码
      product_package: [], //包装清单
      watchshell_material: [], //表壳材质
      watchband_material: '', //表带材质
      mechanism: '手动机械', //机芯类型
      diameter: '', //表直径
      fastener_type: '', //表扣类型
      waterproof: '', //防水
      guarantee: '', //保修功能
      producer: '', //产地
      fineness_description: '', //成色描述
      product_specification: { //产品规格
        longness: '', //长
        width: '', //宽
        height: '', //高
        size: '', //尺寸
        other: '', //其他
      },
      inside_structure: '', //内里结构
    },
    goodsCate: [],
    secondCate: [],
    thirdCate: [],
    xiLie: [],
    index: -1,
    index2: -1,
    index3: -1,
    index4: -1,
    pIndex: 0, //商品传的index


    token_pic: '',
    pic_lis: [],
    key_pic: '',
    qiniu_key: [],
    count: 9,
    detailedList: [{
      name: '肩带',
      state: 1
    }, {
      name: '钥匙',
      state: 0
    }, {
      name: '锁',
      state: 0
    }, {
      name: '盒子',
      state: 1
    }, {
      name: '小票',
      state: 0
    }, {
      name: '保卡',
      state: 0
    }, {
      name: '国检证书',
      state: 0
    }, {
      name: '发票',
      state: 0
    }, {
      name: '说明书',
      state: 0
    }, {
      name: '防尘袋',
      state: 0
    }, {
      name: '身份卡',
      state: 0
    }],
    product_inp: '', //包装清单input
    biaoke_inp: '', //表壳
    CaseMaterial: [{
      name: '精钢',
      state: 1
    }, {
      name: '玫瑰金',
      state: 0
    }, {
      name: '黄金',
      state: 0
    }, {
      name: '白金',
      state: 1
    }, {
      name: '镶钻',
      state: 0
    }, {
      name: '后镶钻',
      state: 0
    }],
    BandMaterial: [{
      name: '精钢',
      state: 1
    }, {
      name: '玫瑰金',
      state: 0
    }, {
      name: '黄金',
      state: 0
    }, {
      name: '白金',
      state: 1
    }, {
      name: '镶钻',
      state: 0
    }, {
      name: '后镶钻',
      state: 0
    }, {
      name: '牛皮',
      state: 0
    }, {
      name: '鳄鱼皮',
      state: 0
    }, {
      name: '橡胶',
      state: 0
    }, {
      name: '后配',
      state: 0
    }],
    BandMaterial_inp: '',
    business_id: ''
  },

  onLoad: function (options) {
    console.log('onload', options)
    this.setData({
      current_page: options.current_page,
      pIndex: options.index ? options.index : 0
    })
    let _this = this;
    if (options.id) {
      if (options.fuc) {
        wx.setNavigationBarTitle({
          title: '商品详情',
        })
        _this.setData({
          disabled: true
        })
      } else {
        wx.setNavigationBarTitle({
          title: '编辑商品',
        })
      }
      wx.showLoading({
        title: '加载中',
      })
      if (options.fuc) {
        infoGoodsDetail({
          token: app.globalData.token,
          id: options.id
        }).then(res => {
          if (res.data.code == 666) {
            var data = res.data.data;
            var Images = [];
            data.image.forEach(item => {
              Images.push(item.pic_url);
            })
            _this.setData({
              addEdit: data,
              isChecked: data.product_cate,
              isChecked2: data.maintenance,
              isChecked3: data.is_onsale,
              isChecked4: data.is_network,
              isEdit: true,
              showImages: Images,
              'addEdit.stock': 1,
              'addEdit.page': 1
            })
          } else if (res.data.code == 111111) {
            wx.showToast({
              title: '登录失效，请重新登录',
              icon: 'none',
            })
            setTimeout(function () {
              wx.reLaunch({
                url: '/pages/user/login/login',
              })
            }, 1500)
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
          wx.hideLoading();
        })
      } else {
        productDetail({
          token: app.globalData.token,
          id: options.id
        }).then(res => {
          if (res.data.code == 666) {
            var data = res.data.data;
            var Images = [];
            data.image.forEach(item => {
              Images.push(item.pic_url);
            })
            _this.setData({
              addEdit: data,
              isChecked: data.product_cate,
              isChecked2: data.maintenance,
              isChecked3: data.is_onsale,
              isChecked4: data.is_network,
              isEdit: true,
              showImages: Images,
              'addEdit.stock': 1,
              'addEdit.page': 1
            })
          } else if (res.data.code == 111111) {
            wx.showToast({
              title: '登录失效，请重新登录',
              icon: 'none',
            })
            setTimeout(function () {
              wx.reLaunch({
                url: '/pages/user/login/login',
              })
            }, 1500)
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
          wx.hideLoading();
        })
      }
    }
    addMerchant({
      token: app.globalData.token
    }).then(res => {
      if(res.data.code == 666){
      _this.setData({
        productFineness: res.data.data.product_fineness,
        perchaseSource: res.data.data.perchase_source,
        warehouseCondition: res.data.data.warehouse_condition,
        business_id: res.data.data.business_id
      })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },

  //选择图片
  addImg: function () {
    var that = this
    console.log(that.data.showImages.len)
    wx.chooseImage({
      count: that.data.count,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        var tempFilePaths = res.tempFilePaths;
        var showImages = that.data.showImages.concat(tempFilePaths)
        if (15 - tempFilePaths.length > 9) {
          that.setData({
            count: 9
          })
        } else {
          that.setData({
            count: 15 - tempFilePaths.length
          })
        }
        that.setData({
          showImages: showImages,
        })
        getToken().then(res => {
          that.setData({
            token_pic: res.data.data.token,
          })
          that.uplod(tempFilePaths, 20)
        })
      }
    })
  },
  //图片上传到七牛
  uplod: function (obj, num) {
    console.log(num)
    var that = this
    var showImages = that.data.showImages
    for (var i = 0; i < obj.length; i++) {
      var timestamp = Date.parse(new Date()) + [i]
      var type = obj[i].substring(obj[i].lastIndexOf("."), obj[i].length);
      var keyy = timestamp + type
      wx.uploadFile({
        url: 'https://upload.qiniup.com',
        filePath: obj[i],
        name: 'file', //文件的key 
        header: {
          "Content-Type": "multipart/form-data"
        },
        formData: {
          token: that.data.token_pic,
          key: keyy
        }, //传递参数
        success: function (res) {
          var data = JSON.parse(res.data)
          var singleImg = {};
          if (that.data.isEdit) {
            singleImg.id = '';
          }
          if (num != 20) {
            console.log(res)
            that.data.addEdit.image[num].pic_url = data.key
            that.data.addEdit.image[num].id = 0
            console.log(that.data.addEdit.image[num])
          } else {
            var image = that.data.addEdit.image
            singleImg.pic_url = data.key
            image.push(singleImg)
          }
          that.setData({
            addEdit: that.data.addEdit
          })
          console.log(that.data.addEdit)
        }
      })
    }
  },
  //替换img
  changeImg: function (e) {
    console.log(e.currentTarget.dataset.key)
    var inde = e.currentTarget.dataset.key
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        var tempFilePaths = res.tempFilePaths;
        var showImages = that.data.showImages.splice(inde, 1, tempFilePaths[0])
        that.setData({
          showImages: that.data.showImages
        })
        console.log(that.data.showImages)
        getToken().then(res => {
          that.setData({
            token_pic: res.data.data.token,
          })
          that.uplod(tempFilePaths, inde)
        })
      }
    })
  },
  // 成色
  toCommQuality(e) {
    this.data.addEdit.finess_value = this.data.productFineness[e.detail.value].value
    this.data.addEdit.product_condition = this.data.productFineness[e.detail.value].id
    this.setData({
      pro_index: e.detail.value
    })
    console.log(this.data.addEdit)
  },
  //库存状态
  toProductStatus(e) {
    this.data.addEdit.wc_value = this.data.warehouseCondition[e.detail.value].value
    this.data.addEdit.is_shelf = this.data.warehouseCondition[e.detail.value].id
    this.setData({
      war_index: e.detail.value
    })
  },
  // 采购类别
  toPurchaseCate(e) {
    this.data.addEdit.procurement_value = this.data.perchaseSource[e.detail.value].value
    this.data.addEdit.procurement_status = this.data.perchaseSource[e.detail.value].id
    this.setData({
      per_index: e.detail.value
    })
    this.productSn()
  },
  //商品类别
  toGoodsCate() {
    var that = this
    var toGoodsCate_lis = {
      cag_title: that.data.addEdit.cag_title,
      category_id: that.data.addEdit.category_id,
      type_title: that.data.addEdit.type_title,
      type_id: that.data.addEdit.type_id,
      brand_title: that.data.addEdit.brand_title,
      brand_id: that.data.addEdit.brand_id,
      systematics_title: that.data.addEdit.systematics_title,
      systematics_id: that.data.addEdit.systematics_id
    }
    wx.navigateTo({
      url: '/pages/addGood/goodsCate/goodsCate?lis=' + JSON.stringify(toGoodsCate_lis),
    })
  },
  //商品货号
  productSn() {
    var a = String(new Date().getTime())
    var that = this
    if (that.data.addEdit.procurement_status && that.data.addEdit.category_id) {
      var leibie = ''
      var pinpai = ''
      if (that.data.addEdit.procurement_status == 1) {
        leibie = 'A'
      }
      if (that.data.addEdit.procurement_status == 2) {
        leibie = 'B'
      }
      if (that.data.addEdit.procurement_status == 3) {
        leibie = 'Z'
      }
      if (that.data.addEdit.procurement_status == 4) {
        leibie = 'Y'
      }
      if (that.data.addEdit.category_id == 1) {
        pinpai = 'B'
      }
      if (that.data.addEdit.category_id == 2) {
        pinpai = 'W'
      }
      if (that.data.addEdit.category_id == 3) {
        pinpai = 'J'
      }
      if (that.data.addEdit.category_id == 4) {
        pinpai = 'X'
      }
      var num = that.data.business_id + leibie + pinpai + a.substring(8)
      that.setData({
        'addEdit.product_sn': num
      })
    }
  },
  // 是否上架
  checkboxOnsale(e) {
    console.log('eee', e)
    if (e.detail.value.length > 0) {
      this.setData({
        'addEdit.is_onsale': Number(e.detail.value[0]),
        isChecked3: 1
      })
    } else {
      this.setData({
        'addEdit.is_onsale': 2,
        isChecked3: 2
      })
    }
  },
  //是否入全网
  checkboxNetwork(e) {
    if (e.detail.value.length > 0) {
      this.setData({
        'addEdit.is_network': 1,
        isChecked4: 1
      })
    } else {
      this.setData({
        'addEdit.is_network': 2,
        isChecked4: 0
      })
    }
    console.log(e)
    console.log('入全网', this.data.addEdit.is_network)
  },
  //所在门店
  toInStore() {
    wx.navigateTo({
      url: 'inStore/inStore',
    })
  },
  //采购员
  toBuyer() {
    let store_id = this.data.addEdit.store_id ? this.data.addEdit.store_id : '';
    wx.navigateTo({
      url: 'buyer/buyer?store_id=' + store_id,
    })
  },
  //商品类型选择
  typeSelected(e) {
    this.setData({
      'addEdit.product_cate': Number(e.detail.value),
      isChecked: Number(e.detail.value)
    })
  },

  //商品货号
  bindProductSn(e) {
    if (/^([a-zA-Z0-9])+$/.test(e.detail.value)) {
      this.setData({
        'addEdit.product_sn': e.detail.value
      })
    } else {
      wx.showToast({
        title: '商品货号请填写字母和数字',
        icon: 'none',
      })
      this.setData({
        'addEdit.product_sn': e.detail.value.substring(0, e.detail.value.length - 1)
      })
      return
    }
  },

  //基础信息复制
  handleInfoCopy(e) {
    let content = e.target.dataset.content
    wx.setClipboardData({
      data: content,
      success() {
        wx.showToast({
          title: '复制成功',
        })
      }
    })
    console.log('content', content);
  },

  //商品名称
  bindProductName(e) {
    this.setData({
      'addEdit.product_name': e.detail.value
    })
  },
  //商品自带编码
  bindProductCode(e) {
    this.setData({
      'addEdit.product_code': e.detail.value
    })
  },
  //存放坐标
  bindCoordinate(e) {
    this.setData({
      'addEdit.coordinate': e.detail.value
    })
  },
  //预警库龄
  bindLibraryAge(e) {
    this.setData({
      'addEdit.library_age': e.detail.value
    })
  },
  //商品描述
  bindSynopsis(e) {
    this.setData({
      'addEdit.synopsis': e.detail.value
    })
  },
  //备注
  bindRemark(e) {
    this.setData({
      'addEdit.remark': e.detail.value
    })
  },
  //商品采购价
  bindCostPrice(e) {
    if (/^(\d?)+(\.\d{0,2})?$/.test(e.detail.value)) {
      this.setData({
        'addEdit.cost_price': e.detail.value
      })
    } else {
      wx.showToast({
        title: '商品采购价请填写数字',
        icon: 'none',
      })
      this.setData({
        'addEdit.cost_price': e.detail.value.substring(0, e.detail.value.length - 1)
      })
      return
    }

  },
  //额外费用
  bindAdditional(e) {
    if (/^(\d?)+(\.\d{0,2})?$/.test(e.detail.value)) {
      this.setData({
        'addEdit.additional': e.detail.value
      })
    } else {
      wx.showToast({
        title: '额外费用请填写数字',
        icon: 'none',
      })
      this.setData({
        'addEdit.additional': e.detail.value.substring(0, e.detail.value.length - 1)
      })
      return
    }
  },
  //商品来源
  bindProducSource(e) {
    this.setData({
      'addEdit.product_source': e.detail.value
    })
  },
  //销售价
  bindPrice(e) {
    if (/^(\d?)+(\.\d{0,2})?$/.test(e.detail.value)) {
      this.setData({
        'addEdit.price': e.detail.value
      })
    } else {
      wx.showToast({
        title: '销售价请填写数字',
        icon: 'none',
      })
      this.setData({
        'addEdit.price': e.detail.value.substring(0, e.detail.value.length - 1)
      })
      return
    }
  },
  //同行价
  bindPeerPrice(e) {
    if (/^(\d?)+(\.\d{0,2})?$/.test(e.detail.value)) {
      this.setData({
        'addEdit.peer_price': e.detail.value
      })
    } else {
      wx.showToast({
        title: '同行价请填写数字',
        icon: 'none',
      })
      this.setData({
        'addEdit.peer_price': e.detail.value.substring(0, e.detail.value.length - 1)
      })
      return
    }
  },
  //活动价
  activityPrice(e) {
    if (/^(\d?)+(\.\d{0,2})?$/.test(e.detail.value)) {
      this.setData({
        'addEdit.activity_price': e.detail.value
      })
    } else {
      wx.showToast({
        title: '活动价请填写数字',
        icon: 'none',
      })
      this.setData({
        'addEdit.activity_price': e.detail.value.substring(0, e.detail.value.length - 1)
      })
      return
    }
  },
  //市场价
  marketPrice(e) {
    if (/^(\d?)+(\.\d{0,2})?$/.test(e.detail.value)) {
      this.setData({
        'addEdit.market_price': e.detail.value
      })
    } else {
      wx.showToast({
        title: '市场价请填写数字',
        icon: 'none',
      })
      this.setData({
        'addEdit.market_price': e.detail.value.substring(0, e.detail.value.length - 1)
      })
      return
    }
  },
  //商品保养情况
  maintenanceSelected(e) {
    this.setData({
      'addEdit.maintenance': Number(e.detail.value)
    })
  },
  //适用人群
  groupPeople(e) {
    this.setData({
      'addEdit.group_people': e.detail.value
    })
  },
  //机芯类型
  mechanism_btn(e) {
    this.setData({
      'addEdit.mechanism': e.detail.value
    })
  },
  //弹框
  commQuality(e) {
    console.log('e', e)
    let show = e.currentTarget.dataset.type;
    if (show == '0') {
      this.setData({
        showCommQuality: true,
      })
    } else if (show == '1') {
      this.setData({
        showLibraryAge: true,
      })
    } else if (show == '2') {
      this.setData({
        showTradePrice: true,
      })
    }

  },
  // 关闭弹框
  closed() {
    this.setData({
      showCommQuality: false,
      showLibraryAge: false,
      showTradePrice: false,
    })
  },
  // 页面显示
  onShow: function () {
    var pages = getCurrentPages();
    console.log('onshow')
  },
  // 包装清单输入
  product_inp(e) {
    this.setData({
      product_inp: e.detail.value
    })
  },
  //表壳材质
  biaoke_inp(e) {
    this.setData({
      biaoke_inp: e.detail.value
    })
  },
  //保存
  save(e) {
    var _this = this;
    var lis_line = _this.data.detailedList
    var qingdan = []
    var biaoke = []
    var biaodai = []
    var biaolis = _this.data.CaseMaterial
    var biaodai_lis = _this.data.BandMaterial
    for (var i = 0; i < lis_line.length; i++) {
      if (lis_line[i].state == 1) {
        qingdan.push(lis_line[i].name)
      }
    }
    for (var j = 0; j < biaolis.length; j++) {
      if (biaolis[j].state == 1) {
        biaoke.push(biaolis[j].name)
      }
    }
    for (var k = 0; k < biaodai_lis.length; k++) {
      if (biaodai_lis[k].state == 1) {
        biaodai.push(biaodai_lis[k].name)
      }
    }
    if (_this.data.product_inp != '') {
      qingdan.push(_this.data.product_inp)
    }
    if (_this.data.biaoke_inp != '') {
      biaoke.push(_this.data.biaoke_inp)
    }
    if (_this.data.BandMaterial_inp != '') {
      biaodai.push(_this.data.BandMaterial_inp)
    }
    _this.data.addEdit.product_package = qingdan
    _this.data.addEdit.watchshell_material = biaoke
    _this.data.addEdit.watchband_material = biaodai
    console.log(_this.data.addEdit)
    return;
    if (_this.data.addEdit.image.length == 0) {
      wx.showToast({
        title: '请上传图片',
        icon: 'none',
        mask: true
      })
      return
    }
    if (_this.data.isChecked == 0 && _this.data.addEdit.finess_value == '') {
      wx.showToast({
        title: '请选择商品成色',
        icon: 'none',
        mask: true
      })
      return
    }
    if (_this.data.addEdit.is_shelf == '') {
      wx.showToast({
        title: '请选择库存状态',
        icon: 'none',
        mask: true
      })
      return
    }
    if (_this.data.addEdit.procurement_status == '') {
      wx.showToast({
        title: '请选择采购类别',
        icon: 'none',
        mask: true
      })
      return
    }
    if (_this.data.addEdit.category_id == '') {
      wx.showToast({
        title: '请选择商品类别',
        icon: 'none',
        mask: true
      })
      return
    }
    if (_this.data.addEdit.synopsis == '成色： \n\n颜色： \n\n规格： \n\n材质：\n\n包装清单：\n\n ') {
      wx.showToast({
        title: '请填写商品描述',
        icon: 'none',
        mask: true
      })
      return
    }
    if (_this.data.addEdit.product_source && (/^1(3|4|5|7|8)\d{9}$/.test(_this.data.addEdit.product_source))) {
      wx.showToast({
        title: '商品来源处请输入正确的手机号',
        icon: 'none'
      })
      _this.setData({
        sourceFocus: true
      })
      return
    }
    if (_this.data.addEdit.price == '') {
      wx.showToast({
        title: '请填写销售价',
        icon: 'none',
        mask: true
      })
      return
    }
    if (_this.data.addEdit.peer_price == '') {
      wx.showToast({
        title: '请填写同行价',
        icon: 'none',
        mask: true
      })
      return
    }
    addEditProduct({
      ...this.data.addEdit,
      token: app.globalData.token
    }).then(res => {
      console.log('保存', res)
      if (res.data.code == 666) {
        wx.showToast({
          title: '添加成功',
          icon: 'none',
          mask: true,
          success: function (res) {
            if (e.currentTarget.dataset.id == 0) {
              wx.setStorageSync('controlType', 'goodControl');
              wx.setStorageSync('del-add-data', 'add')
              wx.switchTab({
                url: '../goodControl/goodControl'
              })
            } else {
              wx.redirectTo({
                url: '/pages/addGood/addGood',
              })
            }
          },
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          mask: true,
        })
      }
    })

  },
  //编辑操作
  //删除商品
  delete() {
    var _this = this;
    wx.showModal({
      title: '删除',
      content: '是否确认删除',
      confirmColor: '#000',
      success: function (e) {
        console.log(e)
        if (e.confirm) {
          delGoods({
            token: app.globalData.token,
            id: _this.data.addEdit.id
          }).then(res => {
            if (res.data.code == 666) {
              wx.showToast({
                title: '删除成功',
              })
              wx.setStorageSync('controlType', 'goodControl');
              wx.setStorageSync('del-add-data', 'del')
              setTimeout(function () {
                wx.switchTab({
                  url: '/pages/goodControl/goodControl',
                })
              }, 1500)

            } else if (res.data.code == 111111) {
              wx.showToast({
                title: '登录失效，请重新登录',
                icon: 'none',
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/user/login/login',
                })
              }, 1500)
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                mask: true
              })
            }
          })
        }
      }
    })

  },
  //保存编辑
  saveEdit() {
    var _this = this;
    console.log(_this.data.addEdit.image)
    if (_this.data.addEdit.image.length == 0) {
      wx.showToast({
        title: '请上传图片',
        icon: 'none',
        mask: true
      })
      return
    }
    if (_this.data.addEdit.is_shelf == '') {
      wx.showToast({
        title: '请选择库存状态',
        icon: 'none',
        mask: true
      })
      return
    }
    if (_this.data.addEdit.procurement_status == '') {
      wx.showToast({
        title: '请选择采购类别',
        icon: 'none',
        mask: true
      })
      return
    }
    if (_this.data.addEdit.category_id == '') {
      wx.showToast({
        title: '请选择商品类别',
        icon: 'none',
        mask: true
      })
      return
    }
    if (_this.data.addEdit.synopsis == '') {
      wx.showToast({
        title: '请填写商品描述',
        icon: 'none',
        mask: true
      })
      return
    }
    if (_this.data.addEdit.product_source && !(/^1(3|4|5|7|8)\d{9}$/.test(_this.data.addEdit.product_source))) {
      wx.showToast({
        title: '请填写正确的商品来源',
        icon: 'none'
      })
      _this.setData({
        sourceFocus: true
      })
      return
    }
    if (_this.data.addEdit.price == '') {
      wx.showToast({
        title: '请填写销售价',
        icon: 'none',
        mask: true
      })
      return
    }
    if (_this.data.addEdit.peer_price == '') {
      wx.showToast({
        title: '请填写同行价',
        icon: 'none',
        mask: true
      })
      return
    }
    let addEdit = this.data.addEdit;
    var _this = this;
    addEdit.token = app.globalData.token;
    console.log(addEdit)
    saveEdit(addEdit).then(res => {
      console.log(res)
      if (res.data.code == 666) {
        var pages = getCurrentPages();
        var currPage = pages[pages.length - 1]; //当前页面
        var prevPage = pages[pages.length - 2]; //上一个页面
        console.log(prevPage)
        var productItems = 'productItems[' + _this.data.pIndex + ']';
        //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
        prevPage.setData({
          [productItems]: _this.data.addEdit
        })
        wx.setStorageSync('controlType', 'goodControl');
        wx.switchTab({
          url: '../goodControl/goodControl',
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          mask: true,
        })
      }
    })
  },
  onUnload() {
    wx.setStorageSync('controlType', 'goodControl');
    console.log(wx.getStorageSync('del-add-data'))
  },

  // 包装清单
  detailedBtn(e) {
    var index = e.currentTarget.dataset.index;
    var that = this
    if (that.data.detailedList[index].state == 0) {
      that.data.detailedList[index].state = 1
    } else {
      that.data.detailedList[index].state = 0
    }
    that.setData({
      detailedList: that.data.detailedList
    })
  },
  //表壳材质
  CaseMaterialBtn(e) {
    var index = e.currentTarget.dataset.index;
    var that = this
    if (that.data.CaseMaterial[index].state == 0) {
      that.data.CaseMaterial[index].state = 1
    } else {
      that.data.CaseMaterial[index].state = 0
    }
    that.setData({
      CaseMaterial: that.data.CaseMaterial
    })
  },
  //表带材质
  BandMaterial(e) {
    var index = e.currentTarget.dataset.index;
    var that = this
    if (that.data.BandMaterial[index].state == 0) {
      that.data.BandMaterial[index].state = 1
    } else {
      that.data.BandMaterial[index].state = 0
    }
    that.setData({
      BandMaterial: that.data.BandMaterial
    })
  },
  //图片删除
  img_delete(e) {
    var index = e.currentTarget.dataset.index
    var node = this.data.showImages
    node.splice(index, 1)
    this.setData({
      showImages: this.data.showImages
    })
  },
  //图片查看
  img_look(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    wx.previewImage({
      current: that.data.showImages[index], // 当前显示图片的http链接
      urls: that.data.showImages // 需要预览的图片http链接列表
    })
  },
  // 颜色录入
  add_color(e) {
    this.setData({
      'addEdit.color': e.detail.value
    })
  },
  //材质录入
  material_inp(e) {
    this.setData({
      'addEdit.material': e.detail.value
    })
  },
  //成色描述
  fineness_description(e) {
    this.setData({
      'addEdit.fineness_description': e.detail.value
    })
  },
  //规格宽
  product_width(e) {
    this.setData({
      'addEdit.product_specification.width': e.detail.value
    })
  },
  //规格高
  product_height(e) {
    this.setData({
      'addEdit.product_specification.height': e.detail.value
    })
  },
  //规格长
  product_logness(e) {
    this.setData({
      'addEdit.product_specification.longness': e.detail.value
    })
  },
  //规格肩带
  product_size(e) {
    this.setData({
      'addEdit.product_specification.size': e.detail.value
    })
  },
  //规格
  product_other(e) {
    this.setData({
      'addEdit.product_specification.other': e.detail.value
    })
  },
  //内里结构
  insideStructure(e) {
    this.setData({
      'addEdit.inside_structure': e.detail.value
    })
  },
  //表径
  diameter_inp(e) {
    this.setData({
      'addEdit.diameter': e.detail.value
    })
  },
  //表带材质input
  BandMaterial_inp(e) {
    this.setData({
      BandMaterial_inp: e.detail.value
    })
  },
  //表扣类型
  fastenerType(e) {
    this.setData({
      'addEdit.fastener_type': e.detail.value
    })
  },
  //防水性能
  waterproof(e) {
    this.setData({
      'addEdit.waterproof': e.detail.value
    })
  },
  //保修
  guarantee(e) {
    this.setData({
      'addEdit.guarantee': e.detail.value
    })
  },
  //产地
  producer(e) {
    this.setData({
      'addEdit.producer': e.detail.value
    })
  }
})